package com.yincheng.oes.business.controller;

import com.yincheng.oes.business.model.dto.StudentPaperDTO;
import com.yincheng.oes.business.model.dto.StudentSearchResultDTO;
import com.yincheng.oes.business.model.query.StudentPaperReq;
import com.yincheng.oes.business.model.query.StudentSearchReq;
import com.yincheng.oes.business.service.impl.StudentServiceImpl;
import com.yincheng.oes.business.student.QueryParamBean;
import com.yincheng.oes.business.student.QueryResultRowBean;
import com.yincheng.oes.business.student.StudentListService;
import com.yincheng.oes.business.util.Tools;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Author: yangsy
 * @Contact: 446948434@qq.com
 * @Date: 2021/9/5 17:15
 * @Version: 1.0
 * @Description: 学员管理界面--1.学员管理列表 2.学员试卷列表
 */
@Controller
@RequestMapping("/studentManager")
public class StudentManagerController {

    /**
     * 日志
     */
    private static final Logger logger = LogManager.getLogger(StudentManagerController.class);

    @Autowired
    private StudentListService studentListService;

    @Autowired
    private StudentServiceImpl studentServiceImpl;

    /**
     * 学生列表数据
     *
     * @return 试图path
     */
    @RequestMapping(value = "/studentListPage.html", method = RequestMethod.GET)
    public String quesListPage() {
        return "business/student/studentlist";
    }

    @RequestMapping(value = "/studentSearchPage.html", method = RequestMethod.GET)
    public String studentSearchPage() {
        return "business/group/studentlist";
    }

    /**
     * 学生推送的学生列表页面
     *
     * @param model model
     * @param paperId 试卷id
     * @return 试图path
     */
    @RequestMapping(value = "/studentPushPage.html", method = RequestMethod.GET)
    public String studentPushPage(Model model, String paperId) {
        model.addAttribute("paperId", paperId);
        return "business/paper/studentlist";
    }

    @RequestMapping(value = "/editStudentInfoPage.html", method = RequestMethod.GET)
    public ModelAndView editAskPage(ModelAndView view, String userId) {

        view.addObject("form", studentListService.selectByUserId(userId));
        view.addObject("assistantList", studentListService.getAssistantList());
        view.setViewName("/business/student/student-edit");
        return view;
    }

    /**
     * 根据参数获取学生列表
     *
     * @param bean QueryParamBean
     * @return 学员分页列表
     */
    @RequestMapping(value = "/getList.json", method = RequestMethod.GET)
    @ResponseBody
    public String list(QueryParamBean bean) {
        Pair<Integer, List<QueryResultRowBean>> pair = studentListService.getListByParam(bean);
        return Tools.toEasyuiDataGrid(pair.getLeft(), pair.getRight());
    }

    /**
     * 更新学员信息
     *
     * @param bean QueryParamBean
     */
    @RequestMapping(value = "/updateStudentInfo.json", method = RequestMethod.POST)
    public void updateStudentInfo(QueryParamBean bean) {
        studentListService.updateStudentInfo(bean);
    }

    /**
     * 根据姓名或者手机号查询学生列表
     *
     * @param req StudentPaperReq
     * @return 学生列表
     */
    @RequestMapping(value = "/searchStudent.json", method = RequestMethod.POST)
    @ResponseBody
    public List<StudentSearchResultDTO> searchStudent(StudentSearchReq req) {
        logger.debug("start searchStudent req is {}", req);
        return studentServiceImpl.searchStudents(req);
    }

    /**
     * 学员管理-试卷查询界面
     *
     * @return 试图path
     */
    @RequestMapping(value = "/studentPaperListPage.html", method = RequestMethod.GET)
    public String studentPaperPage() {
        return "business/student/studentPaper/studentPaperList";
    }

    /**
     * 查询学员的考试列表
     * @param req StudentPaperReq
     * @return 分页考试列表
     */
    @RequestMapping(value = "/searchStudentPapers.json", method = RequestMethod.POST)
    @ResponseBody
    public String searchStudentPapers(StudentPaperReq req) {
        logger.debug("start searchStudentPapers, req is {}", req);
        Pair<Integer, List<StudentPaperDTO>> pair = studentServiceImpl.getListByParam(req);
        return Tools.toEasyuiDataGrid(pair.getLeft(), pair.getRight());
    }

}
